'''
Created on Feb 26, 2016

@author: JimmyMo
'''
import random
import datetime
import utility
import re
import ast

cf = {
      "%dt":"dt()",
      "%ip":"ip()",
      "%s":"ss(lst)"
#       "%d":"dd(L"
      }

def process(x, y):
    if x == "%dt":
        return "dt()" 
    elif x == "%ip":
        return "ip()"
    elif x == "%s":
        return "ss('%s')" % y
    elif x == "%d":
        return "dd('%s')" % y
    elif x == "%f":
        return "ff('%s')" % y
    else:
        return "'_unkown_'"
    
def analyse_log(log_pattern):
    p = re.compile("(%\w+)(?:{(.+?)})?")
    li = p.findall(log_pattern)
    liab = list(map(translate, li))
    if (len(liab)) > 0:
        s = utility.fill_single_quote(p.sub("%s", log_pattern)) + ' % ' + utility.fill_bracket(", ".join(liab))
    else:
        s = utility.fill_single_quote(p.sub("%s", log_pattern))
    
#     r = ast.literal_eval(s)
    r = eval(s)
    return r

def translate(tp):
    x = tp[0]
    y = tp[1]
    return process(x, y)

inc_current_v = 0
def inc(options=None):
    start_v = 1
    step_v = 1
    if not options:
        tmp = options.split(',')
        start_v = int(tmp[0])
        step_v = int(tmp[1])
        
    return 
def ff(options=None):
    min_v = 10
    max_v = 20
    if not options:
        tmp = options.split(',')
        min_v = int(tmp[0])
        max_v = int(tmp[1])
    return random.uniform(min_v, max_v)
def dd(options=None):
    min_v = 1
    max_v = 10
    if not options:
        tmp = options.split(',')
        min_v = int(tmp[0])
        max_v = int(tmp[1])
    return random.randint(min_v, max_v)
def ss(options=""):
    lst = options.split("|")
    lst = [utility.fill_single_quote(elem) for elem in lst]
    return random.choice(lst)
def dt():
#     return datetime.datetime.now().strftime('%b %d %y %H:%M:%S')
    return datetime.datetime.now().strftime('%b %d %H:%M:%S')
def ip():
    return random.choice(["localhost", "192.168.0.1", "192.168.0.2", "192.168.0.3", "176.168.0.1"])

class abc(object):
    def __new__(self, *args, **kwargs):
        return object.__new__(self, *args, **kwargs)
if __name__ == "__main__":
    print("abc")
